package com.supermali.creater.img;

import com.supermali.util.FileUtil;
import com.supermali.util.ImgConvertUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImgLoader {

    private static Map<Enum,ImgHelper> imgs;

    public static Map<Enum, ImgHelper> getImgs() {
        return imgs;
    }

    public ImgLoader() {
        this.imgs = new HashMap<>();
    }

    public static ImgHelper getImgHelper(Enum key){
        return imgs.get(key);
    }

    /**
     * 加载地图元素
     * */
    public void loadMapFactor(){
        loadsmallPerson();
        loadBackground();
        loadbigPerson();
        loadbigBulletPerson();
        loadBullet();
        loadMonistor();
        loadHinder();
        loadFinish();
    }

    private void loadHinder(){
        // 加载地板
        ImgHelper floor = createImgHelper("/img/map/hinder/floor.png");
        imgs.put(ImgKey.Hinder.FLOOR, floor);
        // 加载可顶的问号
        ImgHelper question = createImgHelper("/img/map/hinder/Question.png");
        imgs.put(ImgKey.Hinder.QUESTION, question);

        ImgHelper hit_question = createImgHelper("/img/map/hinder/hit_question.png");
        imgs.put(ImgKey.Hinder.HitQuestion, hit_question);
        //
        ImgHelper smallPipe = createImgHelper("/img/map/hinder/small_pipe.png");
        imgs.put(ImgKey.Hinder.SMALL_PIPE, smallPipe);
        ImgHelper middlePipe = createImgHelper("/img/map/hinder/middle_pipe.png");
        imgs.put(ImgKey.Hinder.MIDDLE_PIPE, middlePipe);
        ImgHelper largePipe = createImgHelper("/img/map/hinder/large_pipe.png");
        imgs.put(ImgKey.Hinder.LARGE_PIPE, largePipe);
        // 实体砖块
        ImgHelper solid = createImgHelper("/img/map/hinder/solid.png");
        imgs.put(ImgKey.Hinder.SOLID, solid);
        // 红色砖块
        ImgHelper redBrick = createImgHelper("/img/map/hinder/redbrick.png");
        imgs.put(ImgKey.Hinder.RED_BRICK, redBrick);
    }
    /**
     * 加载人物相关图片
     * */
    private void loadsmallPerson(){
        // 加载陆地人物
        // 前进
        List<String> runImgIndex = Arrays.asList(
                "/img/map/npc/person/mali/small/runImgIndex-0.png",
                "/img/map/npc/person/mali/small/runImgIndex-1.png",
                "/img/map/npc/person/mali/small/runImgIndex-2.png");
        ImgHelper forward = createImgHelper(runImgIndex);
        imgs.put(ImgKey.SmallPerson.FORWARD,forward);
        // 静止向右
        ImgHelper terminateRight = createImgHelper("/img/map/npc/person/mali/small/terminate.png");
        imgs.put(ImgKey.SmallPerson.TERMINATE, terminateRight);
        // 右跳跃
        ImgHelper jumpRight = createImgHelper("/img/map/npc/person/mali/small/jump.png");
        imgs.put(ImgKey.SmallPerson.JUMP, jumpRight);
        // 下蹲
        imgs.put(ImgKey.SmallPerson.DOWN, terminateRight);

        // 坐下
        ImgHelper sitdown = createImgHelper("/img/map/npc/person/mali/small/sitdown.png");
        imgs.put(ImgKey.SmallPerson.SITDOWN, sitdown);
        // 拥抱
        ImgHelper hug = createImgHelper("/img/map/npc/person/mali/small/hug.png");
        imgs.put(ImgKey.SmallPerson.HUG, hug);

    }
    /**
     * 加载人物相关图片
     * */
    private void loadbigPerson(){
        // 加载陆地人物
        // 前进
        List<String> runImgIndex = Arrays.asList(
                "/img/map/npc/person/mali/big/runImgIndex-0.png",
                "/img/map/npc/person/mali/big/runImgIndex-1.png",
                "/img/map/npc/person/mali/big/runImgIndex-2.png");
        ImgHelper forward = createImgHelper(runImgIndex);
        imgs.put(ImgKey.BigPerson.FORWARD,forward);
        // 静止向右
        ImgHelper terminateRight = createImgHelper("/img/map/npc/person/mali/big/terminate.png");
        imgs.put(ImgKey.BigPerson.TERMINATE, terminateRight);
        // 右跳跃
        ImgHelper jumpRight = createImgHelper("/img/map/npc/person/mali/big/jump.png");
        imgs.put(ImgKey.BigPerson.JUMP, jumpRight);
        // 下蹲
        ImgHelper down = createImgHelper("/img/map/npc/person/mali/big/down.png");
        imgs.put(ImgKey.BigPerson.DOWN, down);
        // 坐下
        ImgHelper sitdown = createImgHelper("/img/map/npc/person/mali/big/sitdown.png");
        imgs.put(ImgKey.BigPerson.SITDOWN, sitdown);
        // 拥抱
        ImgHelper hug = createImgHelper("/img/map/npc/person/mali/big/hug.png");
        imgs.put(ImgKey.BigPerson.HUG, hug);
    }

    private void loadbigBulletPerson(){
        // 加载陆地人物
        // 前进
        List<String> runImgIndex = Arrays.asList(
                "/img/map/npc/person/mali/big_bullet/runImgIndex-0.png",
                "/img/map/npc/person/mali/big_bullet/runImgIndex-1.png",
                "/img/map/npc/person/mali/big_bullet/runImgIndex-2.png");
        ImgHelper forward = createImgHelper(runImgIndex);
        imgs.put(ImgKey.BigBulletPerson.FORWARD,forward);
        // 静止向右
        ImgHelper terminateRight = createImgHelper("/img/map/npc/person/mali/big_bullet/terminate.png");
        imgs.put(ImgKey.BigBulletPerson.TERMINATE, terminateRight);
        // 右跳跃
        ImgHelper jumpRight = createImgHelper("/img/map/npc/person/mali/big_bullet/jump.png");
        imgs.put(ImgKey.BigBulletPerson.JUMP, jumpRight);
        // 下蹲
        ImgHelper down = createImgHelper("/img/map/npc/person/mali/big_bullet/down.png");
        imgs.put(ImgKey.BigBulletPerson.DOWN, down);
        // 坐下
        ImgHelper sitdown = createImgHelper("/img/map/npc/person/mali/big_bullet/sitdown.png");
        imgs.put(ImgKey.BigBulletPerson.SITDOWN, sitdown);
        // 拥抱
        ImgHelper hug = createImgHelper("/img/map/npc/person/mali/big_bullet/hug.png");
        imgs.put(ImgKey.BigBulletPerson.HUG, hug);
    }
    /**
     * 加载monistor相关的图片
     * */
    private void loadMonistor(){
        // 生长蘑菇
        ImgHelper growthmushroom = createImgHelper("/img/map/npc/monistor/growthmushroom.png");
        imgs.put(ImgKey.Monistor.GROWTH_MUSHROOM, growthmushroom);
        // 可发炮弹的花
        ImgHelper bulletFlower = createImgHelper("/img/map/npc/monistor/bullet_flower.png");
        imgs.put(ImgKey.Monistor.BULLET_FLOWER, bulletFlower);
        // 蘑菇怪
        ImgHelper mushroomMonistor = createImgHelper("/img/map/npc/monistor/mushroom_monistor.png");
        imgs.put(ImgKey.Monistor.MUSHROOM_MONISTOR, mushroomMonistor);
    }

    /**
     * 加载bullet相关的图片
     * */
    private void loadBullet(){
        // 生长蘑菇
        ImgHelper circle = createImgHelper("/img/map/npc/bullet/circle.png");
        imgs.put(ImgKey.Bullet.CIRCLE, circle);
    }
    /**
     * 加载结束标志相关的
     * */
    private void loadFinish(){
        // 第一关结束旗杆
        ImgHelper first_finish = createImgHelper("/img/map/finish/flagpole.png");
        imgs.put(ImgKey.FINISH_FLAG.FIRST_FINISH, first_finish);
        // 第一关房子
        ImgHelper house = createImgHelper("/img/map/finish/home.png");
        imgs.put(ImgKey.FINISH_FLAG.HOUSE, house);
    }
    /**
     * 加载背景图片
     * */
    private void loadBackground(){
        // 地板之下的蓝色部分
        ImgHelper floorDown = createImgHelper("/img/map/background/floorDown.png");
        imgs.put(ImgKey.Background.FLOOR_DOWN, floorDown);
        // 加载天空
        ImgHelper sky = createImgHelper("/img/map/background/sky.png");
        imgs.put(ImgKey.Background.SKY, sky);
        // 小草丛
        ImgHelper grass = createImgHelper("/img/map/background/grass.png");
        imgs.put(ImgKey.Background.GRASS, grass);
        // 云彩
        ImgHelper cloud = createImgHelper("/img/map/background/cloud.png");
        imgs.put(ImgKey.Background.CLOUD, cloud);
        // 小山坡
        ImgHelper largeHillside = createImgHelper("/img/map/background/hillside/large_hillside.png");
        imgs.put(ImgKey.Background.BIG_HILLSIDE, largeHillside);
        ImgHelper smallHillside = createImgHelper("/img/map/background/hillside/small_hillside.png");
        imgs.put(ImgKey.Background.SMALL_HILLSIDE, smallHillside);
        // 第一关结束时的旗帜
        ImgHelper flagcircle = createImgHelper("/img/map/background/flagcircle.png");
        imgs.put(ImgKey.Background.FLAG_CIRCLE, flagcircle);
        ImgHelper whiteFlag = createImgHelper("/img/map/background/whiteFlag.png");
        imgs.put(ImgKey.Background.WHITE_FLAG, whiteFlag);
        // 加载首页图
        ImgHelper logo = createImgHelper("/img/map/background/index/logo.png");
        imgs.put(ImgKey.Background.LOGO, logo);
        // 加载选项
        ImgHelper playOption = createImgHelper("/img/map/background/index/playOption.png");
        imgs.put(ImgKey.Background.PLAY_OPTION, playOption);
        // 加载loading
        ImgHelper loading = createImgHelper("/img/map/background/loading/loading.png");
        imgs.put(ImgKey.Background.LOADING, loading);

        ImgHelper breakBrick = createImgHelper("/img/map/background/break_brick.png");
        imgs.put(ImgKey.Background.BREAK_BRICK, breakBrick);

        ImgHelper thin_coin = createImgHelper("/img/map/background/thin_coin.png");
        imgs.put(ImgKey.Background.THIN_COIN, thin_coin);
    }

    private ImgHelper createImgHelper(String path){
        ImgHelper imgHelper = createImgHelper(path, false);
        return imgHelper;
    }
    private ImgHelper createImgHelper(String path, boolean isMirror){
        try {
            ImgHelper imgHelper = new ImgHelper(1);
            InputStream inputStream = this.getClass().getResourceAsStream(path);
            byte[] bytes = FileUtil.readFileToByte(inputStream);
            byte[][] bytes1 = new byte[1][];
            bytes1[0] = bytes;
            imgHelper.setBytes(bytes1);
            ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
            BufferedImage read = ImageIO.read(arrayInputStream);
            arrayInputStream.close();
            BufferedImage[] images = new BufferedImage[1];
            if(isMirror) {
                images[0] = ImgConvertUtil.mirror(read);
            }else {
                images[0]=read;
            }
            imgHelper.setBufferedImage(images);
            return imgHelper;
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private ImgHelper createImgHelper(List<String> paths){
        ImgHelper imgHelper = createImgHelper(paths, false);
        return imgHelper;
    }

    /**
     * @param isMirror 图像是否镜像
     * */
    private ImgHelper createImgHelper(List<String> paths,boolean isMirror){
        try {
            int len = paths.size();
            ImgHelper imgHelper = new ImgHelper(len);
            byte[][] bytes1 = new byte[len][];
            BufferedImage[] images = new BufferedImage[len];
            for(int i=0;i<len;i++){
                String path = paths.get(i);
                InputStream inputStream = this.getClass().getResourceAsStream(path);
                byte[] bytes = FileUtil.readFileToByte(inputStream);
                bytes1[i] = bytes;
                ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
                BufferedImage read = ImageIO.read(arrayInputStream);
                arrayInputStream.close();
                if(isMirror) {
                    images[i] = ImgConvertUtil.mirror(read);
                }else {
                    images[i]=read;
                }
            }
            imgHelper.setBytes(bytes1);
            imgHelper.setBufferedImage(images);
            return imgHelper;
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
